const fs = require('fs')
const { v4: uuidv4 } = require('uuid');
import { connection } from '../my_sql.js/index'


function zxl(router) {

    router.get('/demo', ctx => {
        ctx.body = '2111111'
    })
    //获取全部商品或某一个
    router.get('/getAllGoods', async ctx => {
        let { goods_id, num = 1 } = ctx.request.query

        let sql_str = 'select * from goods'
        let sql_str1 = `select * from goods Limit 0,${num}`
        let sql_str2 = 'select * from cailiao_goods'
        let sql_str3 = 'select * from goods_cailiao_img'
        if (goods_id) {
            sql_str1 = `select * from goods where id = ${goods_id}`
            sql_str2 = `select * from cailiao_goods where goods_id = ${goods_id}`
            sql_str3 = `select * from goods_cailiao_img where goods_id = ${goods_id}`
        }
        let aa = await new Promise((res, rej) => {
            connection.query(sql_str1, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
        let total = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })

        let bb = await new Promise((res, rej) => {
            connection.query(sql_str2, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })



        let cc = await new Promise((res, rej) => {
            connection.query(sql_str3, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })





        let allData = []
        for (let i = 0; i < aa.length; i++) {
            let oneData = aa[i]
            let caiLiao = []
            let goods_imgs = []
            for (let j = 0; j < bb.length; j++) {
                if (aa[i].id == bb[j].goods_id) {
                    caiLiao.push(bb[j])
                }
            }
            for (let j = 0; j < cc.length; j++) {
                if (aa[i].id == cc[j].goods_id) {
                    goods_imgs.push(cc[j])
                }
            }
            oneData.caiLiao = caiLiao
            oneData.goods_imgs = goods_imgs
            allData.push(oneData)
        }

        if (goods_id) {
            let caiLiaoIdArr = []
            for (let i = 0; i < bb.length; i++) {
                caiLiaoIdArr.push(bb[i].id)
            }
            let qqq = caiLiaoIdArr.join(',')
            let sql_str_cailiao = `select path,id from goods_cailiao_img where cailiao_id in (${qqq})`
            let dd = await new Promise((res, rej) => {
                connection.query(sql_str_cailiao, (err, result, fileds) => {
                    if (err) rej(err)
                    res(result)
                })
            })

            allData[0].cailiao_img = dd
        }
        ctx.body = {
            total: total.length,
            allData
        }

    })

    //加入购物车
    router.post('/addCart', async ctx => {

        const { goods_id, goods_title, goods_cailiao, goods_price, goods_num, goods_img, goods_kuaidi, cailiao_id } = ctx.request.body
        let is_exit = false
        let aa = new Promise((res, rej) => {
            let sql_str = "select * from cart where user_id = 1"
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                for (let i = 0; i < result.length; i++) {
                    if (result[i].cailiao_id == cailiao_id) {
                        res(true)
                        is_exit = true
                    }
                }
            })
        })

        if (is_exit) {
            let sql_str = "update cart set goods_num = goods_num * 1 + '" + goods_num + "' where cailiao_id = '" + cailiao_id + "'"
            ctx.body = new Promise((res, rej) => {
                connection.query(sql_str, (err, result, fileds) => {
                    if (err) rej(err)
                    res({
                        code: 1,
                        msg: '加入购物车成功'
                    })
                })
            })
        } else {
            // console.log(88888888);
            let id = uuidv4()
            let user_id = 1
            let sql_str = "insert into cart (id,goods_id,goods_title,goods_cailiao,goods_price,goods_num,goods_img,goods_kuaidi,user_id,cailiao_id) values (?,?,?,?,?,?,?,?,?,?) "
            ctx.body = await new Promise((res, rej) => {
                connection.query(sql_str, [id, goods_id, goods_title, goods_cailiao, goods_price, goods_num, goods_img, goods_kuaidi, user_id, cailiao_id], (err, result, fileds) => {
                    if (err) rej(err)
                    res({
                        code: 1,
                        msg: '加入购物车成功'
                    })
                })
            })
        }



    })


    //获取购物车数据
    router.get('/getCart', async ctx => {
        let sql_str = "select * from cart where user_id = 1"
        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
    })


    //更新购物车数据
    router.post('/updateCart', async ctx => {
        let cart_arr = ctx.request.body

        if (cart_arr.length == 0) {
            let sql_str = "delete from cart where user_id = 1"
            ctx.body = await new Promise((res, rej) => {
                connection.query(sql_str, (err, result, fileds) => {
                    if (err) rej(err)
                    res(true)
                })
            })
        } else {
            let sql_str = "delete from cart where user_id = 1"
            let is_delete = await new Promise((res, rej) => {
                connection.query(sql_str, (err, result, fileds) => {
                    if (err) rej(err)
                    res(true)
                })
            })

            let user_id = 1
            let u_sql_str = "insert into cart (id,goods_id,goods_title,goods_cailiao,goods_price,goods_num,goods_img,goods_kuaidi,user_id) values"
            for (let i = 0; i < cart_arr.length; i++) {
                const { goods_id, goods_title, goods_cailiao, goods_price, goods_num, goods_img, goods_kuaidi } = cart_arr[i]
                let id = uuidv4()
                if (i == cart_arr.length - 1) {
                    u_sql_str = u_sql_str + "('" + id + "','" + goods_id + "',' " + goods_title + "',' " + goods_cailiao + "','" + goods_price + "','" + goods_num + "','" + goods_img + "','" + goods_kuaidi + "',' " + user_id + "')"
                } else {
                    u_sql_str = u_sql_str + "('" + id + "','" + goods_id + "',' " + goods_title + "',' " + goods_cailiao + "','" + goods_price + "','" + goods_num + "','" + goods_img + "','" + goods_kuaidi + "',' " + user_id + "'),"
                }
            }

            // console.log(u_sql_str);
            ctx.body = await new Promise((res, rej) => {
                connection.query(u_sql_str, (err, result, fileds) => {
                    if (err) rej(err)
                    res({
                        code: 1,
                        msg: '加入购物车成功'
                    })
                })
            })
        }


    })

    //更新地址
    // router.post('/updateAddress', async ctx => {
    //     let address_arr = ctx.request.body

    //     if (address_arr.length == 0) {
    //         let sql_str = "delete from dizhi where user_id = 1"
    //         ctx.body = await new Promise((res, rej) => {
    //             connection.query(sql_str, (err, result, fileds) => {
    //                 if (err) rej(err)
    //                 res(true)
    //             })
    //         })
    //     } else {
    //         let sql_str = "delete from dizhi where user_id = 1"
    //         let is_delete = await new Promise((res, rej) => {
    //             connection.query(sql_str, (err, result, fileds) => {
    //                 if (err) rej(err)
    //                 res(true)
    //             })
    //         })

    //         let user_id = 1
    //         let u_sql_str = "insert into dizhi (id,name,tel,province,addressDetail,city,county,isDefault,user_id) values"
    //         for (let i = 0; i < address_arr.length; i++) {
    //             const { name, tel, province, addressDetail, city, county, isDefault } = address_arr[i]
    //             let id = uuidv4()
    //             if (i == address_arr.length - 1) {
    //                 u_sql_str = u_sql_str + "('" + id + "','" + name + "',' " + tel + "',' " + province + "','" + addressDetail + "','" + city + "','" + county + "','" + isDefault + "',' " + user_id + "')"
    //             } else {
    //                 u_sql_str = u_sql_str + "('" + id + "','" + name + "',' " + tel + "',' " + province + "','" + addressDetail + "','" + city + "','" + county + "','" + isDefault + "',' " + user_id + "'),"
    //             }
    //         }
    //     ctx.body = await new Promise((res, rej) => {
    //         connection.query(u_sql_str, (err, result, fileds) => {
    //             if (err) rej(err)
    //             res({
    //                 code: 1,
    //                 msg: '更新成功'
    //             })
    //         })
    //     })
    // }
    // })



    //更新地址
    router.post('/updateAddress', async ctx => {
        const { name, tel, province, addressDetail, city, county, isDefault, id } = ctx.request.body
        console.log(isDefault);
        if (isDefault == 'true') {
            let sql_str = "update dizhi set isDefault = false where user_id = 1"
            let is_ok = await new Promise((res, rej) => {
                connection.query(sql_str, (err, result, fileds) => {
                    if (err) rej(err)
                    res(true)
                })
            })
        }
        let sql_str = "update dizhi  set name = ? , tel = ? , province = ? , addressDetail = ? , city = ? , county = ? , isDefault = ?  where id = '" + id + "' "

        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str, [name, tel, province, addressDetail, city, county, isDefault], (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
    })

    //删除地址
    router.post('/deleteAddress', async ctx => {
        const { id } = ctx.request.body
        let sql_str = "delete from dizhi where id = '" + id + "' "
        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
    })

    //新增地址
    router.post('/addAddress', async ctx => {
        let id = uuidv4()
        console.log(id);
        let user_id = 1
        const { name, tel, province, addressDetail, city, county, isDefault } = ctx.request.body
        console.log(isDefault);
        if (isDefault == true) {
            let sql_str = "update dizhi set isDefault = 'false' where user_id = 1"
            console.log(sql_str);
            let is_ok = await new Promise((res, rej) => {
                connection.query(sql_str, (err, result, fileds) => {
                    if (err) rej(err)
                    res(true)
                })
            })
        }
        let u_sql_str = "insert into dizhi (id,name,tel,province,addressDetail,city,county,isDefault,user_id) values ('" + id + "','" + name + "',' " + tel + "',' " + province + "','" + addressDetail + "','" + city + "','" + county + "','" + isDefault + "',' " + user_id + "')"
        console.log(u_sql_str);
        ctx.body = await new Promise((res, rej) => {
            connection.query(u_sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res({
                    code: 1,
                    msg: '更新成功'
                })
            })
        })
    })

    //获取用户所有地址
    router.get('/getAddress', async ctx => {
        let user_id = '1'
        let sql_str = "select * from dizhi where user_id = '" + user_id + "'"
        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
    })
    //获取用户某一个地址
    router.get('/getAddressById', async ctx => {
        let id = ctx.request.query.id
        let sql_str = "select * from dizhi where  id = '" + id + "'"
        console.log(sql_str);
        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
    })

    //获取默认地址
    router.get('/getDefaultAddress', async ctx => {
        let user_id = 1
        let sql_str = "select * from dizhi where user_id = '" + user_id + "' and isDefault = 'true'"
        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(result)
            })
        })
    })

    //更新默认地址
    router.post('/updateAddressStatus', async ctx => {
        let { id } = ctx.request.body
        let sql_str = "update dizhi set isDefault = 'false' where user_id = 1"
        let is_ok = await new Promise((res, rej) => {
            connection.query(sql_str, (err, result, fileds) => {
                if (err) rej(err)
                res(true)
            })
        })

        let sql_str2 = "update dizhi set isDefault = 'true' where id = '" + id + "'"
        ctx.body = await new Promise((res, rej) => {
            connection.query(sql_str2, (err, result, fileds) => {
                if (err) rej(err)
                res(true)
            })
        })
    })

    //新增订单
    // router.post('/addDingdan', async ctx => {
    //     let id = new uuidv4()
    //     const {dizhi_id,}
    //     ctx.body = await new Promise((res, rej) => {
    //         let sql_str = ""
    //         connection.query(sql_str, (err, result, fileds) => {
    //             if (err) rej(err)
    //             res(result)
    //         })
    //     })
    // })
}

export {
    zxl
}